﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.system
{
    public partial class ProfessionAreaType : BaseAdmin
    {
        readonly DALProfessionAreaType theProfessionAreaType = new DALProfessionAreaType();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.ProfessionAreaType);
                if (Request.QueryString["action"] == "del")
                {
                    this.DeleteProfessionArea();
                }
                this.BindGridView();
            }
        }

        void DeleteProfessionArea()
        {
            if (!theProfessionAreaType.isDelete(Request.QueryString["id"]))
            {
                Response.Write("<script> alert('专区已被使用,无法删除!')</script>");
                return;
            }
            else
            {
                theProfessionAreaType.Delete(int.Parse(Request.QueryString["id"]));
                Response.Write("<script> alert('删除成功!')</script>");
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.BindGridView();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.BindGridView();
        }

        protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            tbProfessionAreaType oArea = new tbProfessionAreaType();
            oArea.Name = this.txtArea.Text.Trim();
            theProfessionAreaType.Add(oArea);
            Response.Write("<script> alert('添加成功!')</script>");
            this.txtArea.Text = "";
            this.BindGridView();
        }
        void BindGridView()
        {

            DataSet ds;
            ds = theProfessionAreaType.GetList(" 1=1");
            DataTable table;
            table = ds.Tables[0];
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
            this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
            this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
            this.LblRecordCount.Text = "总计 " + table.Rows.Count.ToString() + "个记录";
            if (table.Rows.Count == 0)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
                this.LblCurrentIndex.Visible = false;
                this.LblPageCount.Visible = false;
                this.LblRecordCount.Visible = false;
            }
            else if (this.GridView1.PageCount == 1)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
            }
            this.btnFirst.CommandName = "1";
            this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
            this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
            this.btnLast.CommandName = this.GridView1.PageCount.ToString();
            this.ddlCurrentPage.Items.Clear();
            if (this.GridView1.PageCount > 0)
            {
                for (int i = 1; i <= this.GridView1.PageCount; i++)
                {
                    this.ddlCurrentPage.Items.Add(i.ToString());
                }
                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
            }
        }
    }
}